package com.jixiantubu.wode.shezhi;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.example.jixiantubu.R;

import com.jixiantubu.bean.UserBean;
import com.jixiantubu.denglu.Login;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.util.LogUtils;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;

import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ShezhiActivity extends Activity implements OnClickListener {
	ImageView shezhifanhui;
	TextView  huancunzishu, huancun,zhanghaoTextView;
	HttpUtils httpUtils;
	MyApplication application;
	Context context;
	private RelativeLayout zhanghao,rl_cache, banben, lianxi, guanyu;

	// 数据来源
	List<UserBean> list = new ArrayList<UserBean>();

	/**
	 * SD卡缓存大小
	 */
	private long mExternCacheSize;

	/**
	 * 外部缓存目录
	 */
	private File mExternalCacheFile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_shezhi);
		// 去掉标题栏
		/*
		 * ActionBar actionBar = getActionBar(); actionBar.hide();
		 */
		initView();
		initData();
		updateData();
	}

	private void initData() {
		zhanghaoTextView.setText(MyApplication.username);
	}

	private void initView() {
		shezhifanhui = (ImageView) findViewById(R.id.wo_shezhi);
		shezhifanhui.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		rl_cache = (RelativeLayout) findViewById(R.id.Setting);
		huancun = (TextView) findViewById(R.id.wo_shezhi_huancun);
		zhanghaoTextView= (TextView) findViewById(R.id.wo_shezhi_zhanghao2);
		banben = (RelativeLayout) findViewById(R.id.wo_shezhi_banben);
		lianxi = (RelativeLayout) findViewById(R.id.wo_shezhi_lianxi);
		guanyu = (RelativeLayout) findViewById(R.id.wo_shezhi_guanyu);
		zhanghao = (RelativeLayout) findViewById(R.id.wo_shezhi_zhanghao);
		huancunzishu = (TextView) findViewById(R.id.wo_shezhi_huancunzishu);
		rl_cache.setOnClickListener(this);
		huancun.setOnClickListener(this);
		banben.setOnClickListener(this);
		lianxi.setOnClickListener(this);
		guanyu.setOnClickListener(this);
		zhanghao.setOnClickListener(this);
	}

	private void dialog() {

		AlertDialog.Builder builder = new Builder(ShezhiActivity.this);
		builder.setMessage("更改账号？");
		builder.setTitle("提示");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Log.e("弹出提示框", "1111111111111111");
				Intent intent = new Intent(ShezhiActivity.this, Login.class);
				startActivity(intent);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				arg0.dismiss();

			}
		});
		builder.create().show();

	}

	private void updateData() {
		FileCalculateAsyncTask task = new FileCalculateAsyncTask(this);
		mExternalCacheFile = new File(
				FilePathConfig.getExternalCachePath(ShezhiActivity.this));
		task.execute(mExternalCacheFile);
		task.setOnResponseListener(new OnResponseListener() {

			@Override
			public void onResponse(String resultString) {

				try {
					mExternCacheSize = Long.valueOf(resultString);
					huancunzishu.setText(FileUtils.formatSize(mExternCacheSize));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void clearCache() {
		AlertDialog.Builder builder = new Builder(ShezhiActivity.this);
		builder.setMessage("清除缓存");
		builder.setTitle("提示");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				final CustomProgressDialog pd = new CustomProgressDialog(
						ShezhiActivity.this, "正在拼命加载数据,请稍等！", R.anim.frame);

				FileDeleteAsyncTask task = new FileDeleteAsyncTask(context);
				task.execute(mExternalCacheFile);
				task.setOnResponseListener(new OnResponseListener() {

					@Override
					public void onResponse(String resultString) {

						pd.dismiss();
						MyApplication.show(ShezhiActivity.this, "清除成功");
						updateData();
					}
				});

			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				arg0.dismiss();

			}
		});
		builder.create().show();
	}

	@Override
	public void onClick(View v) {
		LogUtils.e("jjjjjjj" + v.getId());
		Log.e("seting", "" + R.id.Setting);
		switch (v.getId()) {
		case R.id.Setting:
			LogUtils.e("dian ji qingchu ");
			clearCache();
			break;
		case R.id.wo_shezhi_banben:
			Toast.makeText(ShezhiActivity.this, "最新版本1.0", 1).show();
			break;
		case R.id.wo_shezhi_lianxi:
			Toast.makeText(ShezhiActivity.this, "我们的热线是:400899**", 1).show();
			break;
		case R.id.wo_shezhi_guanyu:
			Toast.makeText(ShezhiActivity.this, "敬请期待", 1).show();
			break;
		case R.id.wo_shezhi_zhanghao:
			Log.e("账号退出判断", "111111111111111");
			dialog();
			break;

		default:
			break;
		}

	}

}
